package com.monster.service;

import com.monster.dao.SongDao;
import com.monster.dao.SongDaoImpl;
import com.monster.dao.UserDao;
import com.monster.dao.UserDaoImpl;
import com.monster.pojo.Song;
import com.monster.util.SongUtil;

import java.util.ArrayList;

public class PlayerServiceImpl implements PlayerService {
    private UserDao ud = new UserDaoImpl();


    /**
     * 获得需要播放的歌曲信息，并将用户的播放列表添加到user_play表中
     * @param key 关键字，代表类型
     * @param id key类型对应的id
     * @return 歌曲信息集合
     */
    @Override
    public ArrayList<Song> getPlaySongs(String key, Long id) {
        ArrayList<Song> songs = SongUtil.getSongs(key, id);
        return songs;
    }

    /**
     * 获得用户收藏的所有歌曲信息以播放，并将用户的播放列表添加到user_play表中
     * @param userId 用户id
     * @return 歌曲信息集合
     */
    @Override
    public ArrayList<Song> getPlaySongs(Long userId) {
        return ud.listLikeSongsByUserId(userId, 1);     // 此处不应分页，但方法已分页！
    }

    @Override
    public boolean addUserPlay(Long userId, Long songId) {
        if (ud.addPlaySong(userId, songId) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 删除user_play表中的用户播放信息
     * @param userId
     * @return 执行成功-true，失败-false
     */
    @Override
    public void deleteUserPlay(Long userId) {
        ArrayList<Long> songsId = ud.listPlaySongsByUserId(userId);
        for (Long songId : songsId) {
            ud.deletePlaySong(userId, songId);
        }
    }

    @Override
    public boolean deleteUserPlay(Long userId, Long songId) {
        if (ud.deletePlaySong(userId, songId) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<Song> listUserPlay(Long userId) {
        ArrayList<Long> songsId = ud.listPlaySongsByUserId(userId);
        ArrayList<Song> songs = new ArrayList<>(songsId.size());
        for (int i = 0; i < songsId.size(); i++) {
            songs.add(SongUtil.getSongs("song", songsId.get(i)).get(0));
        }
        return songs;
    }
}
